<?php
/* SVN FILE: $Id: item_uploademotion.php 247 2009-10-22 15:31:39Z supersmashbrothers $ */
/**
* ibMarket 2
* IP.Board 3.0
* Upload an emotion item
*
* @author $Author: supersmashbrothers $
* @copyright $Copyright$
* @version $Revision: 247 $
* @lastrevision $Date: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @modifiedby $LastChangedBy: supersmashbrothers $
* @lastmodified $LastChangedDate: 2009-10-22 15:31:39 +0000 (Thu, 22 Oct 2009) $
* @filesource $URL: http://ipb-ibmarket.googlecode.com/svn/ibmarket2/trunk/admin/applications_addon/other/ibmarket/items/item_uploademotion.php $
*/

class item_uploademotion extends templateItem
{
	/**
	 * The default name of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultName = "Upload an Emotion";
	
	/**
	 * The default description of the item
	 *
	 * @access	protected
	 * @var		string
	 */
	protected $defaultDescription = "Upload an emotion for people to use.";
	
	// Inherited Properties
	/**
	 * Item setting to allow multiple instances within ibMarket 
	 *
	 * @access	protected
	 * @var		boolean
	 */
	protected $allowMultiple = true;
	
	/**
	 * Constructor to setup the item
	 *
	 * @access	public
	 * @param	ipsRegistry	$registry	IPB registry
	 * @param	array		&$item		The item information
	 */
	public function __construct(ipsRegistry $registry, array &$item)
	{
		parent::__construct($registry, $item);
		
		if($item['item_config'] || $item['item_id'] != 0)
		{
			$this->itemSettings = unserialize($item['item_config']);
		}
		else
		{
			$this->itemSettings['max_size'] = 1500;
			$this->itemSettings['clickable'] = 0;
			$this->itemSettings['overwrite'] = 0;
		}
	}
	
	/**
	 * Additional settings when adding/editing the item
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminForm()
	{
		$form['max_size'] = ipsRegistry::getClass('output')->formSimpleInput('item[max_size]', $this->itemSettings['max_size']);
		$form['clickable'] = ipsRegistry::getClass('output')->formYesNo('item[clickable]', $this->itemSettings['clickable']);
		$form['overwrite'] = ipsRegistry::getClass('output')->formYesNo('item[overwrite]', $this->itemSettings['overwrite']);
		return <<<EOF
				<li>
					<label>{$this->lang->words['emotion_clickable']}</label>
					{$form['clickable']}
				</li>
				<li>
					<label>
						{$this->lang->words['emotion_overwrite']}
						<span class="desctext">{$this->lang->words['emotion_overwrite_desc']}</span>
					</label>
					{$form['overwrite']}
				</li>
				<li>
					<label>
						{$this->lang->words['emotion_max_size']}
						<span class="desctext">{$this->lang->words['emotion_max_size_desc']}</span>
					</label>
					{$form['max_size']}
				</li>

EOF;
	}
	
	/**
	 * Saving to adding/editing the item
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	array	[$input]		Relavent input relating to the item
	 * @return	void
	 */
	public function adminSave(array &$item_info, array $input)
	{
		$setting = array();
		$setting['max_size'] = intval($input['max_size']);
		$setting['clickable'] = intval($input['clickable']) & 1;
		$setting['overwrite'] = intval($input['overwrite']) & 1;
		
		$item_info['item_config'] = serialize($setting);
	}
	
	/**
	 * When quick saving the price and stock
	 *
	 * @access	public
	 * @param	array	&$item_info		The new item information to be stored in the database.
	 * @param	string	$type			Possible values: 'stockprice', more possibly in the future
	 * @return	void
	 */
	public function adminQuickSave(array &$item_info, $type)
	{
		return;
	}
	
	/**
	 * When removing the item from ibMarket
	 *
	 * @access	public
	 * @return	void
	 */
	public function adminRemove()
	{
		return;
	}
	
	/**
	 * Any admin AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function adminAjax()
	{
		return;
	}
	
	/**
	 * Viewing the file edits if any
	 *
	 * @access	public
	 * @return	string|bool	string if there is something to edit, false otherwise
	 */
	public function adminViewFileEdits()
	{
		return false;
	}
	
	/**
	 * Check and modify the intentory item information before storing in the database
	 *
	 * @access	public
	 * @param	array	&$invItem		The new inventory item information to be stored in the database.
	 * @return	bool	true if the item is successfully created, false otherwise
	 */
	public function publicOnCreateItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When using the item
	 * Show a form and validate the form
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	string|bool				string if there is a form to be used, true if successfully used, false otherwise
	 */
	public function publicUseItem(array &$invItem)
	{
		$this->registry->getClass('class_localization')->loadLanguageFile( array( 'public_items' ) );
		
		require_once( IPSLib::getAppDir( 'core' ) . "/modules_admin/posts/emoticons.php" );
		
		$emotion = new admin_core_posts_emoticons();
		
		if($this->request['request_method'] == 'post')
		{
			require_once( IPS_KERNEL_PATH.'classUpload.php' );
			$upload = new classUpload();
			
			$this->request['emotion_text'] = str_replace( '&quot;', "", $this->request['emotion_text']);
			
			$upload->allowed_file_ext  = $emotion->allowed_files;
			$upload->out_file_dir      = $this->settings['upload_dir'];
			$upload->max_file_size     = $this->itemSettings['max_size'];
			$upload->upload_form_field = "upload_emotion";
			$upload->out_file_name     = "emo-".md5(microtime());
			
			$upload->process();
			
			if($upload->error_no)
			{
				$this->registry->getClass('output')->showError('upload_failed');
			}
			
			try
			{
				foreach( new DirectoryIterator( DOC_IPS_ROOT_PATH . '/public/style_emoticons' ) as $_emoticon )
				{
					if ( $_emoticon->isDir() AND ! $_emoticon->isDot() AND $_emoticon->getFilename() != '.svn' )
					{
						if ( is_dir( DOC_IPS_ROOT_PATH . '/public/style_emoticons/' . $_emoticon ) )
						{
							if(!file_exists(DOC_IPS_ROOT_PATH.'/public/style_emoticons/'.$_emoticon."/".$upload->original_file_name) || $this->itemSettings['overwrite'])
							{
								if ( @copy( $upload->saved_upload_name, DOC_IPS_ROOT_PATH.'/public/style_emoticons/'.$_emoticon."/".$upload->original_file_name ) )
								{
									@chmod( DOC_IPS_ROOT_PATH.'/public/style_emoticons/'.$_emoticon."/".$upload->original_file_name, 0777 );
								}
							}
							
							if($old_emo = $this->DB->buildAndFetch(array('select'=>'*','from'=>'emoticons','where'=>"emo_set='".$_emoticon."' AND (typed='".$this->request['emotion_text']."' OR image='".$upload->original_file_name."')")))
							{
								if($this->itemSettings['overwrite'])
								{
									if($old_emo['typed'] == $this->request['emotion_text'])
									{
										$this->DB->update( 'emoticons', array( 'clickable' => $old_emo['clickable'], 'image' => $upload->original_file_name ), "emo_set='".$_emoticon."' AND typed='".$this->request['emotion_text']."'" );
									}
									else
									{
										$this->DB->update( 'emoticons', array( 'clickable' => $old_emo['clickable'], 'typed' => $this->request['emotion_text'] ), "emo_set='".$_emoticon."' AND image='".$upload->original_file_name."'" );
									}
								}
							}
							else
							{
								$this->DB->insert( 'emoticons', array( 'clickable' => $this->itemSettings['clickable'], 'typed' => $this->request['emotion_text'], 'image' => $upload->original_file_name, 'emo_set' => $_emoticon ) );
							}
						}
					}
				}
			} catch ( Exception $e ) {}
			
			@unlink($upload->saved_upload_name);
			
			$this->deleteItems($invItem);
			
			$this->writeLog($invItem, "Uploaded emotion, ".$upload->original_file_name);
			
			ipsRegistry::cache()->rebuildCache('emoticons','global');
			
			$this->registry->getClass('output')->redirectScreen( $this->lang->words['emotions_uploaded'], $this->settings['base_url_with_app'] . "module=inventory" );
		}
		
		$limit = "";
		
		$this->lang->words['emotion_ext'] = sprintf($this->lang->words['emotion_ext'], implode(", ", $emotion->allowed_files));
		$this->lang->words['emotion_limit'] = $this->itemSettings['max_size'] ? sprintf($this->lang->words['emotion_limit'], $this->itemSettings['max_size']) : "";
		
		return <<<EOF
<form id='uploadEmotionForm' enctype="multipart/form-data" method="post" action="{$this->settings['base_url_with_app']}module=inventory&amp;section=item&amp;item={$invItem['i_id']}">
	<div id='uploadEmotion'class='block_wrap'>
		<h3 id='uploadEmotionHeader' class='maintitle'>{$this->item['item_name']}</h3>
		<fieldset class="row2">
			<p class='message unspecific'>
				{$this->lang->words['emotion_limit']}
				<br />
				{$this->lang->words['emotion_ext']}
			</p>
			<ul>
				<li>
					<label>{$this->lang->words['select_an_image_file']}</label>
					<input type="file" size="30" name="upload_emotion" id="upload_emotion" />
				</li>
				<li>
					<label>{$this->lang->words['text_for_emotion']}</label>
					<input type='text' name='emotion_text' value='' size='10' class='input_text' />
				</li>
			</ul>
		</fieldset>
		<fieldset class="submit">
			<input class="input_submit" type="submit" value="{$this->lang->words['upload_emotion']}" name="submit" />
		</fieldset>
	</div>
</form>
EOF;
	}
	
	/**
	 * Any AJAX functionality
	 *
	 * @access	public
	 * @param	array	$invItem		The inventory item
	 * @return	void
	 */
	public function publicAjax(array &$invItem)
	{
		return;
	}
	
	/**
	 * When the item is being sold back
	 *
	 * @access	public
	 * @param	array	&$invItem	The inventory item information
	 * @return	bool				true if successful, false otherwise
	 */
	public function publicOnResoldItem(array &$invItem)
	{
		return true;
	}
	
	/**
	 * When the item is being restocked
	 *
	 * @access	public
	 * @param	array	&$item	The inventory item information
	 * @return	void
	 */
	public function onItemRestock(array &$item)
	{
		return;
	}
}
